package com.software.frontend.videoEdit;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.software.frontend.R;

import ly.img.android.pesdk.VideoEditorSettingsList;
import ly.img.android.pesdk.backend.model.EditorSDKResult;
import ly.img.android.pesdk.backend.model.state.LoadSettings;
import ly.img.android.pesdk.backend.model.state.VideoEditorSaveSettings;
import ly.img.android.pesdk.ui.activity.VideoEditorBuilder;

/**
 * @author yoki
 */
public class VideoEditActivity extends AppCompatActivity {

    private static final int EDITOR_REQUEST_CODE = 101;
    private static final int GALLERY_REQUEST_CODE = 0x69;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_edit);
        invoke();
    }

    /**
     * Opens the device's video gallery to select a video.<br />
     * If the device does not have a gallery app installed, it displays a message saying "No Gallery app installed".<br />
     * The request code used to identify the request when the result is returned.
     */
    private void invoke() {
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
        try {
            startActivityForResult(intent, GALLERY_REQUEST_CODE);
        } catch (ActivityNotFoundException ex) {
            showMessage("No Gallery app installed");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (intent == null) {
            finish();
            return;
        };
        switch (requestCode) {
            case EDITOR_REQUEST_CODE:
                EditorSDKResult result = new EditorSDKResult(intent);
                switch (result.getResultStatus()) {
                    case CANCELED:
                        showMessage("Editor cancelled");
                        finish();
                        break;
                    case EXPORT_DONE:
                        showMessage("Result saved at " + result.getResultUri());
                        finish();
                        break;
                    default:
                        // Do nothing
                }
                break;
            case GALLERY_REQUEST_CODE:
                if (resultCode == Activity.RESULT_OK) {
                    if (intent.getData() != null) {
                        showEditor(intent.getData());
                    } else {
                        showMessage("Invalid Uri");
                    }
                } else if (resultCode == Activity.RESULT_CANCELED) {
                    showMessage("User cancelled selection");
                }
                break;
            default:
                break;
        }
    }

    /**
     * Opens the video editor with the specified Uri as the source.<br />
     * The result will be obtained in onActivityResult() corresponding to EDITOR_REQUEST_CODE.<br />
     * The edited video will be saved to the device's gallery.<br />
     * @param uri The Uri of the video to be loaded and edited.
     */
    private void showEditor(Uri uri) {
        // In this example, we do not need access to the Uri(s) after the editor is closed
        // so we pass false in the constructor
        VideoEditorSettingsList settingsList = new VideoEditorSettingsList(false);
        // Set the source as the Uri of the video to be loaded
        LoadSettings loadSettings = settingsList.getSettingsModel(LoadSettings.class);
        loadSettings.setSource(uri);
        // Save output to Gallery
        VideoEditorSaveSettings videoEditorSaveSettings = settingsList.getSettingsModel(VideoEditorSaveSettings.class);
        videoEditorSaveSettings.setOutputToGallery();
        // Start the video editor using VideoEditorBuilder
        // The result will be obtained in onActivityResult() corresponding to EDITOR_REQUEST_CODE
        VideoEditorBuilder builder = new VideoEditorBuilder(this);
        builder.setSettingsList(settingsList);
        builder.startActivityForResult(this, EDITOR_REQUEST_CODE);
        // Release the SettingsList once done
        settingsList.release();
    }

    public void showMessage(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}